<?php

class Admin_Table_Cron extends Apk_Db_Table
{
	protected $_name      = 'tags';
    protected $_primary   = 'id';
    public $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }


    public function GetCitiesList($a = null, $region_id = null, $lang = 'ru')
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('cities', array('id', 'name' => 'name_'.$lang))
					->order('name_ru');
					
		if(!is_null($region_id)) $sel->where('id_region = ?', $region_id);
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }
    
    public function GetTagByName($tag)
    {
		$sel = $this->getAdapter()->select()
					->from($this->_name, array('id', 'name'))
					->where('name = ?', $tag);
					
		$data = $this->getList($sel)->toArray();		
		
		if(count($data) > 0) return $data[0]['id'];
		else return 0;					
    } 
    
    public function SearchTagsByName($tag)
    {
		$sel = $this->getAdapter()->select()
					->from($this->_name, array('id', 'name'))
					->where("name like '".$tag."%'")
					->order('name')
					->limit(10);
					
		$data = $this->getList($sel)->toArray();
		$ret = array();
		
		if(count($data) > 0)
		foreach($data as $k => $v)
		{
			$ret[$v['id']] = $v['name'];
		}
		
		return $ret;
    }     
           
    public function GetTagsByObjId($object_name = 'articles', $object_id)
    {
        $table_name = 'tags_to_'.$object_name;
        $table_field_name = 'id_'.$object_name;

    	$sel = $this->getAdapter()->select()
					->from($this->_name, array('id', 'name'))
					->join($table_name, $table_name.'.id_tag = tags.id')
					->where($table_name.'.'.$table_field_name.' = ?', $object_id);					
							
		return $this->getList($sel)->toArray();				
    } 
    
    public function GetTagsStringByObjId($object_name = 'articles', $object_id)
    {
		$tags = $this->GetTagsByObjId($object_name, $object_id);
		
		$ret = '';		
		foreach ($tags as $tag) 
		{
			if($ret != '') $ret .= ', ';
			$ret .= $tag['name'];
		}
		return $ret;
    }    

	public function insertStr($tags_str, $object_name = 'articles', $object_id, $user_id)
    {
        $table_name = 'tags_to_'.$object_name;
        $table_field_name = 'id_'.$object_name;
        
    	$tags_arr = explode(',', $tags_str);
    	
    	$this->getAdapter()->delete($table_name, $table_field_name.' = '.$object_id);
        
        if(count($tags_arr) > 0)  
        foreach($tags_arr as $tag) {        	
        	$tag = trim($tag);
        	
        	if($tag == '') continue;
        	
        	$tag_id = $this->GetTagByName($tag);
        	
        	if($tag_id == 0){ 
        		$d = array();       	
	        	$d['name'] = $tag;
	        	$d['add_date'] = time();
	        	$d['modify_date'] = time();
	        	$d['add_user_id'] = $user_id;
	        	$d['modify_user_id'] = $user_id;
	        	
	        	try{
	        		$tag_id = parent::insert($d);
	        	}catch (Exception $e){
	        		//echo 'hello'; die();
	        	}
        	}        	

        	if($tag_id > 0)	 
        	{
        	   $d = array(
        	      'id_tag' => $tag_id,
        	      $table_field_name => $object_id
        	   );
        	   
        	   try{
        	   		$this->getAdapter()->insert($table_name, $d);
	        	}catch (Exception $e){
	        		//echo 'hello'; die();
	        	}        	   		
        	}
        	
        }    			
    }

    public function update(array $data, $id)
    {
		return parent::update($data, 'id = '.$id);
    }

    public function remove($id)
    {
    	parent::delete('id = '.$id);
	}


    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
 			case 'contacts':
            case 'text':
            	$result[$key] = stripslashes($value);
            	break;
            default:
                $result[$key] = $value;
                break;
        }
        return $result;
    }

}
?>